package servlet;

import com.alibaba.fastjson.JSON;
import entity.User;
import service.UserService;
import service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "UserServlet", value = "/UserServlet")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        UserService userService = new UserServiceImpl();
        String opr = request.getParameter("opr");
        if (opr.equals("add")) {
            User user = new User();
            user.setUserName(request.getParameter("name"));
            user.setDiscount(request.getParameter("name"));
            user.setPassword(request.getParameter("pwd"));
            user.setPhone(request.getParameter("phone"));
            int result = userService.addUser(user);
            if (result > 0) {
                response.sendRedirect("frontEnd/login.jsp");
            } else {
                response.getWriter().print("<script>alert('新增用户失败') ;</script>");
            }
        }
        if (opr.equals("login")) {
            String discount = request.getParameter("discount");
            String password = request.getParameter("password");
            User user = userService.login(discount, password);
            if (user != null) {
                request.getSession().setAttribute("user", user);
                request.getSession().setMaxInactiveInterval(60 * 60 * 24 * 10);
                response.sendRedirect("frontEnd/index.jsp");
            } else {
                response.getWriter().print("<script>alert('账号或密码错误，登录失败');location.href='frontEnd/login.jsp'</script>");
            }
        }
        if (opr.equals("retrieve")) {
            String discount = request.getParameter("discount");
            String phone = request.getParameter("phone");
            String password = request.getParameter("password");
            int result = userService.retrievePassword(phone, discount, password);
            if (result > 0) {
                response.sendRedirect("frontEnd/login.jsp");
            } else if (result == -1) {
                response.getWriter().print("<script>alert('手机号注册成功') ;location.href='frontEnd/register.jsp'</script>");
            } else {
                response.getWriter().print("<script>alert('找回密码成功') ;</script>");
            }
        }
        if (opr.equals("updateUser")) {
            String headImg = request.getParameter("headImg");
            String username = request.getParameter("username");
            String address = request.getParameter("address");
            int id = Integer.parseInt(request.getParameter("id"));
            int result = userService.updateUser(headImg,username,address,id);
            User user = userService.login(request.getParameter("discount"), request.getParameter("password"));
            request.getSession().setAttribute("user", user);
            request.getSession().setMaxInactiveInterval(60 * 60 * 24 * 10);
            if(result > 0){
                response.getWriter().print(true);
            }else{
                response.getWriter().print(false);
            }
        }

        if("findAll".equals(opr)){
            List<User> list = userService.findAll();
            response.getWriter().print(JSON.toJSONString(list));
        }

        if("findAllByName".equals(opr)){
            List<User> list = userService.findAllByName(request.getParameter("name"));
            response.getWriter().print(JSON.toJSONString(list));
        }

        if("addAll".equals(opr)){
            User user = new User();
            user.setUserName(request.getParameter("userName"));
            user.setDiscount(request.getParameter("discount"));
            user.setPassword(request.getParameter("password"));
            user.setPhone(request.getParameter("phone"));
            user.setPhone(request.getParameter("headImg"));
            user.setAddress(request.getParameter("address"));
            user.setMoney(Integer.parseInt(request.getParameter("money")));
            user.setScore(Integer.parseInt(request.getParameter("score")));
            if(userService.addUser(user)>0){
                response.getWriter().print("<script>alert('添加成功');location.href='afterEnd/user.jsp'</script>");
            }else {
                response.getWriter().print("<script>alert('添加失败');location.href='afterEnd/user.jsp'</script>");
            }
        }

        if("update".equals(opr)){
            User user = new User();
            user.setUserName(request.getParameter("userName"));
            user.setDiscount(request.getParameter("discount"));
            user.setPassword(request.getParameter("password"));
            user.setPhone(request.getParameter("phone"));
            user.setHeadImg(request.getParameter("headImg"));
            user.setAddress(request.getParameter("address"));
            user.setMoney(Integer.parseInt(request.getParameter("money")));
            user.setScore(Integer.parseInt(request.getParameter("score")));
            user.setId(Integer.parseInt(request.getParameter("id")));
            if(userService.updateById(user)>0){
                response.getWriter().print("<script>alert('修改成功');location.href='afterEnd/user.jsp'</script>");
            }else {
                response.getWriter().print("<script>alert('修改失败');location.href='afterEnd/user.jsp'</script>");
            }
        }

        if("delid".equals(opr)){
            if(userService.delById(Integer.parseInt(request.getParameter("id")))>0){
                response.getWriter().print("true");
            }else {
                response.getWriter().print("false");
            }
        }

        if("findByid".equals(opr)){
            User user = userService.findId(Integer.parseInt(request.getParameter("id")));
            response.getWriter().print(JSON.toJSONString(user));
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
